/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#ifndef UCOP_SINGLETON_H_
#define UCOP_SINGLETON_H_

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif

#include "Ucop/Macros.h"
#include "Ucop/Policies/CreatePolicy.h"
#include "Ucop/Policies/ThreadingModel.h"
#include "Ucop/Policies/LifeTimePolicy.h"


namespace Ucop {

    template<
        class T,
        class ThreadingModel = Policies::SingleThreaded<T>,
        class CreatePolicy = Policies::OperatorNew<T>,
        class LifeTimePolicy = Policies::ObjectLifeTime<T>
    >
    class Singleton
    {
    public:
        static T& Instance(void);

    protected:
        Singleton(void) { }

    private:
        static void _DestroySingleton(void);

        typedef typename ThreadingModel::Lock Guard;
        static T* _Myinstance;
        static bool _Mydestroyed;
        DISALLOW_COPY_AND_ASSIGN(Singleton);
    };

    template<typename T, class ThreadingModel, class CreatePolicy, class LifeTimePolicy>
    T* Singleton<T, ThreadingModel, CreatePolicy, LifeTimePolicy>::_Myinstance = NULL;

    template<typename T, class ThreadingModel, class CreatePolicy, class LifeTimePolicy>
    bool Singleton<T, ThreadingModel, CreatePolicy, LifeTimePolicy>::_Mydestroyed = false;

    template<typename T, class ThreadingModel, class CreatePolicy, class LifeTimePolicy>
    T& Singleton<T, ThreadingModel, CreatePolicy, LifeTimePolicy>::Instance(void)
    {
        if (!_Myinstance)
        {
            // double-checked Locking pattern
            Guard();

            if (!_Myinstance)
            {
                if (_Mydestroyed)
                {
                    _Mydestroyed = false;
                    LifeTimePolicy::OnDeadReference();
                }

                _Myinstance = CreatePolicy::Create();
                LifeTimePolicy::ScheduleCall(&_DestroySingleton);
            }
        }

        return *_Myinstance;
    }

    template<typename T, class ThreadingModel, class CreatePolicy, class LifeTimePolicy>
    void Singleton<T, ThreadingModel, CreatePolicy, LifeTimePolicy>::_DestroySingleton(void)
    {
        CreatePolicy::Destroy(_Myinstance);
        _Myinstance = NULL;
        _Mydestroyed = true;
    }
}

#define INSTANTIATE_SINGLETON_1(TYPE) \
    template class Ucop::Singleton<TYPE, Ucop::Policies::SingleThreaded<TYPE>, Ucop::Policies::OperatorNew<TYPE>, Ucop::Policies::ObjectLifeTime<TYPE> >;

#define INSTANTIATE_SINGLETON_2(TYPE, THREADINGMODEL) \
    template class Ucop::Singleton<TYPE, THREADINGMODEL, Ucop::Policies::OperatorNew<TYPE>, Ucop::Policies::ObjectLifeTime<TYPE> >;

#define INSTANTIATE_SINGLETON_3(TYPE, THREADINGMODEL, CREATIONPOLICY ) \
    template class Ucop::Singleton<TYPE, THREADINGMODEL, CREATIONPOLICY, Ucop::Policies::ObjectLifeTime<TYPE> >;

#define INSTANTIATE_SINGLETON_4(TYPE, THREADINGMODEL, CREATIONPOLICY, OBJECTLIFETIME) \
    template class Ucop::Singleton<TYPE, THREADINGMODEL, CREATIONPOLICY, OBJECTLIFETIME >;

#endif // UCOP_SINGLETON_H_
